package com.monitor.service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Service;

import com.base.service.RedisTemplateService;
import com.config.dao.EquipmentDao;
import com.config.dao.LineConfigDao;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.data.dao.ChargeDao;
import com.data.entity.ChargeDay;
import com.data.entity.ChargeMin;

@Service("monitorService")
public class MonitorServiceImpl implements MonitorService{
	@Resource(name="equipmentDao")
	private  EquipmentDao equipmentDao;
	@Resource(name = "lineConfigDao")
	private LineConfigDao lineConfigDao;
	
	@Resource(name="chargeDao")
	private ChargeDao chargeDao;
	@Resource(name = "redisService")
	private RedisTemplateService redisService; 
	/**
	 * 广智云能耗单机设备刷新
	 * @param equId
	 * @param model
	 */
	@Override
	public void singleRefreshData(int equId,Map<String, Object> model) {
		LocalDate localDate = new DateTime().toLocalDate();
		String belongDate = localDate.toString("yyyy-MM-dd");
		Equipment equ=null;
		if(equId==0){
			equ= equipmentDao.getFirstEquipment();
		}else{
			equ=equipmentDao.get(equId);
		}
		if(equ!=null){
			HashOperations<String, String, Object> op=redisService.hashOperations();
			Object energy= op.get("wz:energy",equ.getEquNo());
			model.put("energy", energy);
			ChargeDay cd=chargeDao.getTotalChargesByDay(equ.getId(),belongDate,belongDate, "N");
			if(cd!=null){
				model.put("relativeComp", cd.getRelativeComp());//电度
				model.put("charge", cd.getCharge());//电费
			}
			model.put("equ", equ);
		}
	}
	
	/**
	 * 广智云整线设备能耗刷新
	 * @param lineId
	 * @param model
	 */
	@Override
	public void lineRefreshData(int lineId, Map<String, Object> model) {
		DecimalFormat  df = new DecimalFormat("0.0");  
		LocalDate localDate = new DateTime().toLocalDate();
		String belongDate = localDate.toString("yyyy-MM-dd");
		LineConfig line=null;
		if(lineId==0){
			line=lineConfigDao.findFirstLine();
		}else{
			line=lineConfigDao.get(lineId);
		}
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		HashOperations<String, String, Object> op=redisService.hashOperations();
		if(line!=null){
			double totalCharges=0.0;double totalEnergy=0.0;
			List<Equipment> equs=equipmentDao.listByLine(line.getId());
			if(equs.size()!=0){
				int i=0;
				for(Equipment e:equs){
					Map<String,Object> emap=new HashMap<String,Object>();
					emap.put("energy", op.get("wz:energy",e.getEquNo()));
					ChargeDay cd=chargeDao.getTotalChargesByDay(e.getId(),belongDate,belongDate,"N");
					if(cd!=null){
						totalCharges+=cd.getCharge();totalEnergy+=cd.getRelativeComp();
						emap.put("relativeComp", cd.getRelativeComp());//电度
						emap.put("charge", cd.getCharge());//电费
					}
					emap.put("glID", i);
					list.add(emap);
					i++;
				}
			}
			model.put("line", line);
			model.put("equEnergy", list);
			model.put("totalCharges", df.format(totalCharges));
			model.put("totalEnergy", df.format(totalEnergy));
		}
	}
	/**
	 * 高能耗设备监控
	 * @param lineId
	 * @param model
	 */
	@Override
	public void highEnergSingle(int equId, Map<String, Object> model) {
		String tstartTime="";String tendTime="";String ystartTime="";String yendTime="";
		List<String> belongTimes=new ArrayList<String>();
		List<Double> charges=new ArrayList<Double>();
		DateTime currentTime = new DateTime();
		String belongDate = currentTime.toString("yyyy-MM-dd");
		int minute = currentTime.getMinuteOfHour();
		if(minute >= 0 && minute < 30){
			//今天此时
			currentTime = currentTime.hourOfDay().roundFloorCopy().withMinuteOfHour(30);
			tstartTime = currentTime.toString("yyyy-MM-dd HH:mm");
			tendTime = tstartTime;
			//昨天此时
			currentTime = currentTime.minusDays(1);
			ystartTime = currentTime.toString("yyyy-MM-dd HH:mm");
			yendTime = ystartTime;
		}else{
			//今天此时
			currentTime = currentTime.hourOfDay().roundFloorCopy().withMinuteOfHour(30);
			tstartTime = currentTime.toString("yyyy-MM-dd HH:mm");
			currentTime = currentTime.hourOfDay().roundFloorCopy().plusHours(1);
			tendTime = currentTime.toString("yyyy-MM-dd HH:mm");
			//昨天此时
			currentTime = currentTime.hourOfDay().roundFloorCopy().minusHours(1).withMinuteOfHour(30).minusDays(1);
			ystartTime = currentTime.toString("yyyy-MM-dd HH:mm");
			currentTime = currentTime.hourOfDay().roundFloorCopy().plusHours(1);
			yendTime = currentTime.toString("yyyy-MM-dd HH:mm");
		}
		Equipment equ=null;
		if(equId==0){
			equ= equipmentDao.getFirstEquipment();
		}else{
			equ=equipmentDao.get(equId);
		}
		if(equ!=null){
			//今天当前小时电度电费
			ChargeMin tcd=chargeDao.getHourChargesByHour(equ.getId(), tstartTime, tendTime, "N");
			if(tcd!=null){
				model.put("t_relativeComp", tcd.getRelativeComp());//电度
				model.put("t_charge", tcd.getCharge());//电费
			}
			//昨日当前小时电度电费
			ChargeMin ycd=chargeDao.getHourChargesByHour(equ.getId(), ystartTime, yendTime, "N");
			if(ycd!=null){
				model.put("y_relativeComp", ycd.getRelativeComp());//电度
				model.put("y_charge", ycd.getCharge());//电费
			}
			//今日总电度电费
			ChargeDay cd=chargeDao.getTotalChargesByDay(equ.getId(),belongDate,belongDate, "N");
			if(cd!=null){
				model.put("total_relativeComp", cd.getRelativeComp());//电度
				model.put("total_charge", cd.getCharge());//电费
			}
			List<ChargeMin> list=chargeDao.getChargeMinsByDay(equ.getId(), belongDate, "N");
			if(list.size()!=0){
				for(ChargeMin cm:list){
					DateTime dt = new DateTime(cm.getBelongTime());
					belongTimes.add(dt.toString("HH:mm"));
					charges.add(cm.getCharge());
				}
			}
			model.put("belongTimes", belongTimes);
			model.put("charges", charges);
			model.put("equ", equ);
		}
	}
}
